﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using Newtonsoft.Json.Serialization;
using System.ComponentModel.DataAnnotations;
using System.Windows;

namespace Enterprises.Wpf.Sample.ViewModel
{
    /// <summary>
    /// ObservableValidator继承 
    /// </summary>
    public partial class ObservableValidatorDemoCtlViewModel : ObservableValidator
    {
        [ObservableProperty]
        [NotifyDataErrorInfo]
        [Required]
        [MinLength(2,ErrorMessage = "至少输入2位")] // Any other validation attributes too...
        [MaxLength(20, ErrorMessage = "至多输入20位")] // Any other validation attributes too...
        private string? userName;

        [ObservableProperty]
        [NotifyDataErrorInfo]
        [Required]
        [Range(18,100, ErrorMessage ="只能输入18-100的数字")]
        private string? age;

        [ObservableProperty]
        [NotifyDataErrorInfo]
        [Required]
        [EmailAddress(ErrorMessage = "邮箱格式不对")]
        private string? email;

        [RelayCommand]
        private void Save()
        {
            if (HasErrors)
            {
                var errors = GetErrors();
                Growl.Error(string.Join("\r\n", errors));
            }
        }

        [RelayCommand]
        private void Submit()
        {
        }
    }
}
